package conf;

import java.io.IOException;
import java.util.List;

public class Config {
    private ConfigDo configDo;

    private String getTypeByFile(String cfgFile)
    {
        String cfgType="xml";
        int dotPos=cfgFile.lastIndexOf(".");
        if (dotPos!=-1)
            return cfgFile.substring(dotPos+1);

        return cfgType;
    }

    public Config(String cfgFile) throws IOException {
        String cfgType=getTypeByFile(cfgFile);
        if (cfgType.equals("xml")) {
            configDo = new XmlConfigDo(cfgFile, "utf-8");
        }
        else if (cfgType.equals("yml")||cfgType.equals("yaml")) {
            configDo = new YamlConfigDo("",cfgFile);
        }
    }

    public Config(String cfgString,String cfgType){
        if (cfgType.equals("xml")) {
            configDo = new XmlConfigDo(cfgString);
        }
        else if (cfgType.equals("yml")||cfgType.equals("yaml")) {
            configDo = new YamlConfigDo(cfgString);
        }
    }

    public List<Object> getNodeList(String name)
    {
        return configDo.getNodeList(name);
    }

    public Object getNode1(String name)
    {
        return configDo.getNode1(name);
    }

    public <T> String getNodeAttr(T t, String key) {
        return configDo.getNodeAttr(t,key);
    }

    public <T> String getNodeText(T t) {
        return configDo.getNodeText(t);
    }

}
